#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 说明：自行将中英文混合文本按照中文、英文分段处理，全部使用`onnx`模式合成语音数据，然后按照原有顺序拼接起来。
# 速度快。
import re,uuid
import sys,os,time
import mix
from flask import Flask, send_file, request, abort
app = Flask("__name__")

name = 'tmp'

@app.route("/api/tts", methods=["POST"])
def api_tts():
	text = request.form['text'].strip()
	if len(text)==0:
		return abort(404)
	try:
		st = time.time()
		global name
		if name != 'tmp':
			os.unlink(name)
		name = '/tmp/'+str(uuid.uuid1())+'.wav'
		res=mix.infer2wav(text,name)
		#dt = time.time()-st
		#print("time cost: "+str(dt))
		return send_file(name)
	except:
		print(sys.exc_info())
		print("请求失败:"+text)
		mix.reset_executors()
		return abort(404)

if __name__ == '__main__':
	try:
		app.run(host="0.0.0.0", port=5000)
		if name != 'tmp':
			os.unlink(name)
	except:
		print(sys.exc_info())
		print("崩溃\n")
